package Reproductor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.Color;
import javax.swing.*;
import java.io.File;
/**
 *
 * @author Pablo
 */
public class Reproductor2 extends javax.swing.JApplet {

    private String filename = "c:/1.mp3";
    MP3AD track = new MP3AD(filename);
    private DefaultListModel df = new DefaultListModel();
    private String[] tracks = new String[100];
    private String[] titles = new String[100];
    private int cont=0, index;
    private FileChooser selector;
    int sw=0; 
    Cronometro reloj;
    /**
     * Initializes the applet Reproductor2
     */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Reproductor2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Reproductor2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Reproductor2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Reproductor2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the applet */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                @Override
                public void run() {                                                           
                    initComponents();
                    reloj = new Cronometro();
                    reloj.setVisible(true);                    
                    reloj.setBounds(300, 80, 130, 50);
                    add(reloj);        
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        open = new javax.swing.JButton();
        previous = new javax.swing.JButton();
        play = new javax.swing.JButton();
        next = new javax.swing.JButton();
        stop = new javax.swing.JButton();
        pantalla = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        imagen = new javax.swing.JLabel();

        getContentPane().setLayout(null);

        open.setText("ABRIR");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });
        getContentPane().add(open);
        open.setBounds(50, 40, 80, 30);

        previous.setText("PREVIOUS");
        previous.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousActionPerformed(evt);
            }
        });
        getContentPane().add(previous);
        previous.setBounds(140, 40, 90, 30);

        play.setText("PLAY");
        play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playActionPerformed(evt);
            }
        });
        getContentPane().add(play);
        play.setBounds(240, 40, 80, 30);

        next.setText("NEXT");
        next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextActionPerformed(evt);
            }
        });
        getContentPane().add(next);
        next.setBounds(330, 40, 70, 30);

        stop.setText("STOP");
        stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopActionPerformed(evt);
            }
        });
        getContentPane().add(stop);
        stop.setBounds(410, 40, 70, 30);

        pantalla.setBackground(new java.awt.Color(255, 255, 255));
        pantalla.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        pantalla.setForeground(new java.awt.Color(255, 255, 255));
        pantalla.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pantalla.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pantalla.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().add(pantalla);
        pantalla.setBounds(50, 80, 220, 60);

        DefaultListModel model = new DefaultListModel();
        lista.setModel(model);
        lista.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaValueChanged(evt);
            }
        });
        lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lista);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(50, 150, 430, 140);

        imagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/The KMPlayer .jpg"))); // NOI18N
        getContentPane().add(imagen);
        imagen.setBounds(0, 0, 530, 350);
    }// </editor-fold>//GEN-END:initComponents

    private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
        selector = new FileChooser();              
        filename = selector.getNameFile().toString();        
        tracks[cont] = filename;
        titles[cont] = selector.getNameFile().getName();
        cont++;        
        lista.setListData(tracks);
        if(cont==1){
            track = new MP3AD(filename);
        }
    }//GEN-LAST:event_openActionPerformed

    private void previousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousActionPerformed
        if(index > 0){
            index = lista.getSelectedIndex();
            index = index - 1;
            lista.setSelectedIndex(index);      
            filename = tracks[index];
            track.close();
            track = new MP3AD(filename);
            track.play();
            pantalla.setText("REPRODUCIENDO  "+titles[index]);
            sw=1;
        }
    }//GEN-LAST:event_previousActionPerformed

    private void playActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playActionPerformed
        if(sw==0) {
            if(filename.length()==0){
                pantalla.setText("ABRA UN ARCHIVO PARA REPRODUCIR");
            }
            else{
                track.play();
                reloj.startCronometro();
                pantalla.setText("REPRODUCIENDO  "+titles[index]);                
                play.setText("PAUSE");
                sw=1;
            }
        } 
        else {
            reloj.pauseaCronometro();
            track.pause();
            play.setText("PLAY");              
            pantalla.setText("PAUSADO  "+titles[index]);
            sw=0;
        }
    }//GEN-LAST:event_playActionPerformed

    private void nextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextActionPerformed
        if(index < cont-1){
            index = lista.getSelectedIndex();
            index = index + 1;
            lista.setSelectedIndex(index);      
            filename = tracks[index];
            track.close();
            track = new MP3AD(filename);
            track.play();
            pantalla.setText("REPRODUCIENDO  "+titles[index]);
            sw=1;
        }
    }//GEN-LAST:event_nextActionPerformed

    private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
        reloj.stopCronometro();
        track.close();
        if(sw==1){            
            play.setText("PLAY");           
            sw=0;
        }
        pantalla.setText("");
    }//GEN-LAST:event_stopActionPerformed

    private void listaValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaValueChanged
        JList source = (JList) evt.getSource();
        filename = source.getSelectedValue().toString();
        index = source.getSelectedIndex();
        track.close();
        track = new MP3AD(filename);
        track.play();
        pantalla.setText("REPRODUCIENDO  "+titles[index]);
        play.setText("PAUSE");
        sw=1;
    }//GEN-LAST:event_listaValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel imagen;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lista;
    private javax.swing.JButton next;
    private javax.swing.JButton open;
    private javax.swing.JLabel pantalla;
    private javax.swing.JButton play;
    private javax.swing.JButton previous;
    private javax.swing.JButton stop;
    // End of variables declaration//GEN-END:variables
}
